require(["common", "coAjax", "queryString", "hash_routie", "title", "href"], function (jSouper, coAjax, QueryString, hash_routie, title, href) {
	var queryString = new QueryString(location.hash);
	App.set("$Cache.goods_info.type", "新品上市");
	title("新品上市");
	var goods_list_url = appConfig.goods.list;
	var page_length;
	var current_page_num;
	//获取指定页的商品
	function get_page_goods(goods_pre_num, current_page_num) {
		coAjax.get(goods_list_url, {
			bus_id: appConfig.bus_id,
			num: goods_pre_num,
			page: current_page_num
		}, function (result) {
				jSouper.ready(function () {
					App.set("goods", result.result);
				});
			});
	}
	hash_routie.on("goodslist", function () {
		//页号
		current_page_num = ~~queryString.get("page");
		//每页显示数量
		var goods_pre_num = ~~queryString.get("num") || appConfig.goods_pre_num || 20;
		App.set("config.goods_pre_num", goods_pre_num);
		App.set("$Cache.goods_pre_num", goods_pre_num);
		jSouper.ready(function (argument) {
			App.set("$Cache.current_page_num", current_page_num);
		});
		get_page_goods(goods_pre_num, current_page_num);
		//获取所有商品数量
		coAjax.get(appConfig.goods.number, {
			bus_id: appConfig.bus_id,
		}, function (result) {
				jSouper.ready(function () {
					var number = ~~result.result
					App.set("$Cache.goods_number", number);
					var _goods_number_list = [];
					_goods_number_list.length = number;
					var _page_num = [];
					//向上取整获取页数
					// console.log(number, goods_pre_num, Math.ceil(number / goods_pre_num));
					_page_num.length = Math.ceil(number / goods_pre_num);
					page_length = _page_num.length - 1;
					App.set("$Cache.goods_number_list", _goods_number_list);
					App.set("$Cache.page_num", _page_num);
				});
			});
		//------------
		// 上一页
		App.set("$Event.prepage", function () {
			console.log("hehe");
			if (current_page_num > 0) {
				current_page_num -= 1;
				get_page_goods(goods_pre_num, current_page_num);
			}
		});
		// 下一页
		App.set("$Event.nextpage", function () {
			console.log(page_length);
			if (current_page_num < page_length) {
				current_page_num += 1;
				get_page_goods(goods_pre_num, current_page_num);
			}
		});
		// 跳转到某一页
		App.set("$Event.jumpToPage", function (e, vi) {
			current_page_num = vi.get("$Index");
			get_page_goods(goods_pre_num, current_page_num);
		});
	});
});